﻿using CardPlay.Model.ActionModel;

namespace CardPlay.Tests.Model.ActionModel
{
    internal class Calculator
    {
        private class History : ActionHistory
        {
            protected override bool IsChangePermitted
            {
                get { return true; }
            }

            protected override object CreateSnapshot()
            {
                return null;
            }

            protected override void OnStateChanged(object priorState)
            {
            }
        }

        public class AddOperation : DelegatingAction
        {
            public AddOperation(Calculator calculator, int value)
            {
                OnExecute = () => { calculator._result += value; };
                OnReverse = () => { calculator._result -= value; };
            }
        }

        public class MultiplyOperation : DelegatingAction<int>
        {
            public MultiplyOperation(Calculator calculator, int value)
            {
                OnExecute = () =>
                {
                    var savedValue = calculator._result;
                    calculator._result *= value;
                    return savedValue;
                };
                OnReverse = savedValue => { calculator._result = savedValue; };
            }
        }

        private readonly History _history = new History();
        private int _result;

        public IActionHistory Operations
        {
            get { return _history; }
        }

        public int Result
        {
            get { return _result; }
        }

        public void Execute(IAction action)
        {
            using (var scope = _history.TransactionScope)
            {
                scope.Execute(action);
            }
        }
    }
}